package com.google.project.client.ProjectManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.UsersDTO;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class PMSelectProject extends DialogBox {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField(provided=true) CellTable<ProjectDTO> ctbProject = new CellTable<ProjectDTO>();
	@UiField Button btnClose;

	interface Binder extends UiBinder<Widget, PMSelectProject> {
	}
	private final ProjectServiceAsync projService = GWT.create(ProjectService.class);
	
	public PMSelectProject() {
		setWidget(binder.createAndBindUi(this));
	}
	public PMSelectProject(UsersDTO currUser) {
		initTable();
		setWidget(binder.createAndBindUi(this));
		bindData(currUser.getUserId(),1);
	}
	/***
	 * lấy về project đang hoạt động có status=1 + userId(login)= pmID
	 */
	public void bindData( final String _userId, int status) {
		try {
			projService.getProjectList(
					new AsyncCallback<List<ProjectDTO>>() {

						@Override
						public void onFailure(Throwable caught) {

						}

						@Override
						public void onSuccess(List<ProjectDTO> result) {
							List<ProjectDTO>  list=new ArrayList<ProjectDTO>();
							for (ProjectDTO projectDTO : result) {
								if(projectDTO.getStatus()==1 && projectDTO.getUserId().equals(_userId)){
									list.add(projectDTO);
								}
							}
							loadData(list);
						}
					});
		} catch (Exception ex) {
			ex.getMessage();
		}

	}
	// Ham tao Cell table
	public void initTable() {
		// STT
		TextColumn<ProjectDTO> indexColumn = new TextColumn<ProjectDTO>() {
			@Override
			public String getValue(ProjectDTO object) {
				return object.getIndex() + "";
			}
		};


		// Ma du an projectCodeColumn
		Column<ProjectDTO, String> projectNameColumn = new Column<ProjectDTO, String>(
				new ClickableTextCell()) {
			@Override
			public String getValue(ProjectDTO object) {
				return object.getProjectName();
			}
		};
		
		projectNameColumn
				.setFieldUpdater(new FieldUpdater<ProjectDTO, String>() {
					@Override
					public void update(int index, ProjectDTO project,
							String value) {
						closeDialog();
						PMEntrypoint.get().PMCreateProjectReport(project);
						
					}
				});

		TextColumn<ProjectDTO> projectCodeColumn = new TextColumn<ProjectDTO>() {
			@Override
			public String getValue(ProjectDTO object) {
				return object.getProjectCode();
			}
		};

		TextColumn<ProjectDTO> startDateColumn = new TextColumn<ProjectDTO>() {
			@Override
			public String getValue(ProjectDTO project) {
				return CommonFuntion.convertToDate(new Date(project
						.getStartDate()));
			}
		};
		TextColumn<ProjectDTO> dueDateColumn = new TextColumn<ProjectDTO>() {
			@Override
			public String getValue(ProjectDTO project) {
				return CommonFuntion.convertToDate(new Date(project
						.getDueDate()));
			}
		};

		ctbProject.setWidth("100%", true);
		ctbProject.setPageSize(5);
		ctbProject.setPageStart(0);
		ctbProject.setStyleName("ttable");

		ctbProject.addColumn(indexColumn, "STT");
		ctbProject.setColumnWidth(indexColumn, 50.0, Unit.PX);

		ctbProject.addColumn(projectCodeColumn, "Mã dự án");
		ctbProject.setColumnWidth(projectCodeColumn, 15.0, Unit.PCT);

		ctbProject.addColumn(projectNameColumn, "Tên dự án");
		projectNameColumn.setCellStyleNames("celltable");
		ctbProject.setColumnWidth(projectNameColumn, 40.0, Unit.PCT);
		projectNameColumn.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		
		
		
		ctbProject.addColumn(startDateColumn, "Ngày bắt đầu");
		ctbProject.setColumnWidth(startDateColumn, 20.0, Unit.PCT);
		ctbProject
				.getColumn(ctbProject.getColumnIndex(startDateColumn))
				.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		ctbProject.addColumn(dueDateColumn, "Ngày kết thúc");
		ctbProject.setColumnWidth(dueDateColumn, 20.0, Unit.PCT);
		ctbProject.getColumn(ctbProject.getColumnIndex(dueDateColumn))
				.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

	}
	public void loadData(List<ProjectDTO> projectList) {
		// Create a data provider.
		ListDataProvider<ProjectDTO> dataProvider = new ListDataProvider<ProjectDTO>();

		// Add the data to the data provider, which automatically pushes it to
		// the widget
		int index=1;
		List<ProjectDTO> list = dataProvider.getList();
		for (ProjectDTO projectDTO : projectList) {
			projectDTO.setIndex(index);
			list.add(projectDTO);
			index++;
		}
		//pager.setDisplay(ctbProject);
		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctbProject);
	}
 void closeDialog(){
	 this.hide();
 }
	@UiHandler("btnClose")
	void onBtnCloseClick(ClickEvent event) {
		this.hide();
	}
}
